# -*- encoding: utf-8 -*-

from odoo import models, fields, api, _
from odoo.addons import base_cw
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_round, float_compare



class account_voucher_remark(models.Model):
    _name = 'account.voucher.remark'
    _description = u'会计凭证摘要'

    @api.model
    def name_search(self, name, args=None, operator='ilike', limit=100):
        args = args or []
        recs = self.browse()
        if name:
            recs = self.search(['|','|', ('name', operator, name), ('code', operator, name), ('short_code', operator, name)] + args, limit=limit)
        if not recs:
            recs = self.search([('name', operator, name)] + args, limit=limit)
        return recs.name_get()

    code = fields.Char(u'编号')
    name = fields.Char(u'摘要', required=True)
    active = fields.Boolean(u'启用', default=True)
    short_code = fields.Char(u'助记码')
    note = fields.Text(u'备注')
    _sql_constraints = [('account_voucher_remark_unique_code', 'unique (code)', u'编号不能重复!'),
                        ('account_voucher_remark_unique_name', 'unique (code)', u'摘要不能重复!')
                        ]

    @api.onchange('name')
    def onchange_name(self):
        if self.name:
            self.short_code = base_cw.public.multi_get_letter(self, self.name)
        else:
            self.short_code = ''
